<?php

class FIN_newsBean
{

   
    
    function getAllNews($db){
    	$sql = "SELECT * FROM ".TBL_NEWS." ORDER BY news_date DESC";
    			
    	$res = $db->get_results($sql);
    	
    	return $res;
    }
	
	function count_news($db,$langid,$key)
    {
		if($key!='')
			$cond = " WHERE nl.tags like '%%$key%%' ";
    	$sql = "SELECT COUNT(n.news_id) as total FROM ".TBL_NEWS." n ".
				"LEFT JOIN ".TBL_NEWS_LANG." nl ON n.news_id = nl.news_id AND nl.lang_id = '$langid' $cond ";
    			
    	$res = $db->get_row($sql);
    	
    	return $res->total;
    }
    function getAllNewsSharePage($db,$langid,$share, $list,$key){
		$page = isset ( $_GET[$list] ) ? intval ( $_GET[$list] ) : 1;
		$rows_per_page= $share;
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
	
		if($key!='')
			$cond = " WHERE nl.tags like '%%$key%%' OR nl.news_title like '%%$key%%' OR nl.news_content like '%%$key%%' OR nl.news_short like '%%$key%%'";
		
		$strSQL = "SELECT *,DATE_FORMAT(n.news_date,'%d/%m/%Y') as date FROM ".TBL_NEWS." n ".
				"LEFT JOIN ".TBL_NEWS_LANG." nl ON n.news_id = nl.news_id AND nl.lang_id = '$langid' ".
				" $cond ORDER BY n.news_date DESC LIMIT $page_start, $rows_per_page";
		
		$res = $db->get_results($strSQL);
		//echo count($res);
		if($res!=""){
				foreach($res as $item){
					$item->news_title = html_entity_decode($item->news_title);
					$item->news_short = html_entity_decode($item->news_short);
					$item->news_content = html_entity_decode($item->news_content);
				}
			}
		return $res;	
    }
    
	
	function getNews($db,$langid, $id)
    {
    	$sql = "SELECT *,DATE_FORMAT(n.news_date,'%d/%m/%Y') as date FROM ".TBL_NEWS." n ".
				"LEFT JOIN ".TBL_NEWS_LANG." l ON n.news_id = l.news_id AND l.lang_id = '$langid' ".
				"WHERE n.news_id='$id'";
    			
    	$res = $db->get_row($sql);
    	if($res!=""){
					$res->news_title = html_entity_decode($res->news_title);
					$res->news_short = html_entity_decode($res->news_short);
					$res->news_content = html_entity_decode($res->news_content);
					$res->tags = html_entity_decode($res->tags);
			}
    	return $res;
    }
    
    function getHomeNews($db,$langid)
    {
    	$sql = "SELECT l.news_title,n.news_id,DATE_FORMAT(n.news_date,'%d/%m/%Y') as date FROM ".TBL_NEWS." n ".
				"LEFT JOIN ".TBL_NEWS_LANG." l ON n.news_id = l.news_id AND l.lang_id = '$langid' ".
				"ORDER BY n.news_date DESC LIMIT 0,".MAX_DEMO_NEWS_HOME;
    			
    	$res = $db->get_results($sql);
    	if($res!=""){
				foreach($res as $item){
					$item->news_title = html_entity_decode($item->news_title);
					//$item->news_short = html_entity_decode($item->news_short);
				//	$item->news_content = html_entity_decode($item->news_content);
				}
			}
    	return $res;
    }
    
	
}
?>